package com.ug_project.spring_dict

//import kotlin.test.*
import com.google.gson.Gson
import com.ug_project.spring_dict.DataAccess.Migrations.DictTables.*
import com.ug_project.spring_dict.DataAccess.Migrations.Users
import com.ug_project.spring_dict.DataAccess.connectDb
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transaction
import org.joda.time.DateTime
import org.junit.Test
import java.io.File
import java.util.*


    data class DictItem(val key: String, val value: String)
    data class DictItems(val items: Array<DictItem>)

    val jsonHomeDir = "jsons/"

//    @Test
    fun main(args: Array<String>) {

        importEnUg()
        importZhUg()
        importUgEn()
        importUgZh()
        importUgUg()

    }

    fun importDictData(jsonFileName: String, table: DictTable) {

        val objects = Gson().fromJson(File(jsonFileName).readText(), Array<DictItems>::class.java)

        transaction(connectDb()) {

            val userId = Users.select { Users.signInName eq "admin" and (Users.signInPassword eq "admin") }.adjustSlice { this.slice(Users.id) }.single()[Users.id]

            objects.forEachIndexed { index, dictItems ->
                println("$index of ${objects.size}")

                table.batchInsert(dictItems.items.filter {  it.key.length < 79}.toList()) {
                    this[table.guid] = UUID.randomUUID().toString().replace("-", "")
                    this[table.createdAt] = DateTime.now()
                    this[table.updatedAt] = DateTime.now()
                    this[table.accessibility] = DictTable.Companion.Accessibility.Available

                    this[table.key] = it.key
                    this[table.value] = it.value
                    this[table.creator] = userId
                }
            }
        }
    }

    fun importEnUg(){
        val jsonName = jsonHomeDir + "enuy.json"
        transaction(connectDb()){
            Dictionaries.insert {
                it[guid] = UUID.randomUUID().toString().replace("-", "")
                it[createdAt] = DateTime.now()
                it[updatedAt] = DateTime.now()
                it[sourceLanguageLabel] = "English"
                it[destinationLanguageLabel] = "ئۇيغۇرچە"
                it[sourceLanguageDirectionIsRtl] = false
                it[destinationLanguageDirectionIsRtl] = true
                it[languagesCode] = DictTable.Companion.DictionaryDirections.EnUg
                it[enabled] = true
            }
        }
        importDictData(jsonName, EnUg)
    }
    fun importZhUg() {
        val jsonName = jsonHomeDir + "chuy.json"
        transaction(connectDb()) {
            Dictionaries.insert {
                it[guid] = UUID.randomUUID().toString().replace("-", "")
                it[createdAt] = DateTime.now()
                it[updatedAt] = DateTime.now()
                it[sourceLanguageLabel] = "中文"
                it[destinationLanguageLabel] = "ئۇيغۇرچە"
                it[sourceLanguageDirectionIsRtl] = false
                it[destinationLanguageDirectionIsRtl] = true
                it[languagesCode] = DictTable.Companion.DictionaryDirections.ZhUg
                it[enabled] = true
            }
        }
        importDictData(jsonName, ZhUg)
    }
    fun importUgEn() {
        val jsonName = jsonHomeDir + "uyen.json"
        transaction(connectDb()) {
            Dictionaries.insert {
                it[guid] = UUID.randomUUID().toString().replace("-", "")
                it[createdAt] = DateTime.now()
                it[updatedAt] = DateTime.now()
                it[sourceLanguageLabel] = "ئۇيغۇرچە"
                it[destinationLanguageLabel] = "English"
                it[sourceLanguageDirectionIsRtl] = true
                it[destinationLanguageDirectionIsRtl] = false
                it[languagesCode] = DictTable.Companion.DictionaryDirections.UgEn
                it[enabled] = true
            }
        }
        importDictData(jsonName, UgEn)
    }
    fun importUgZh() {
        val jsonName = jsonHomeDir + "uych.json"
        transaction(connectDb()) {
            Dictionaries.insert {
                it[guid] = UUID.randomUUID().toString().replace("-", "")
                it[createdAt] = DateTime.now()
                it[updatedAt] = DateTime.now()
                it[sourceLanguageLabel] = "ئۇيغۇرچە"
                it[destinationLanguageLabel] = "中文"
                it[sourceLanguageDirectionIsRtl] = true
                it[destinationLanguageDirectionIsRtl] = false
                it[languagesCode] = DictTable.Companion.DictionaryDirections.UgZh
                it[enabled] = true
            }
        }
        importDictData(jsonName, UgZh)
    }
    fun importUgUg() {
        val jsonName = jsonHomeDir + "uyuy.json"
        transaction(connectDb()) {
            Dictionaries.insert {
                it[guid] = UUID.randomUUID().toString().replace("-", "")
                it[createdAt] = DateTime.now()
                it[updatedAt] = DateTime.now()
                it[sourceLanguageLabel] = "ئۇيغۇرچە"
                it[destinationLanguageLabel] = "ئۇيغۇرچە"
                it[sourceLanguageDirectionIsRtl] = true
                it[destinationLanguageDirectionIsRtl] = true
                it[languagesCode] = DictTable.Companion.DictionaryDirections.UgUg
                it[enabled] = true
            }
        }
        importDictData(jsonName, UgUg)
    }



